import store from '@/store';
import {
	baseUrl
} from '@/utils/setting'
import Vue from 'vue'

const HEADER = {
	'content-type': 'application/json'
}
/**
 * 发送请求
 */
// function baseRequest(url, method, data, loadingText = "加载中...", header, ) {
function baseRequest(url, method, data, header, ) {
	let token = uni.getStorageSync('token') || ''
	return new Promise((reslove, reject) => {
		// if (loadingText) {
		// 	uni.showLoading({
		// 		title: loadingText,
		// 		mask: true
		// 	});
		// }
		uni.request({
			url: `${baseUrl}app/` + url,
			method: method || 'POST',
			header: {
				...HEADER,
				...header,
				'Authorization': token // 添加 token 到请求头
			},
			data: data || {},
			success: (res) => {
				// if (loadingText) {
				// 	uni.hideLoading()
				// }
				if (res.statusCode == 200) {
					reslove(res.data)
				} else {
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
					reject(res)
				}
			},
			fail: (err) => {
				// if (loadingText) {
				// 	uni.hideLoading()
				// }
				uni.showToast({
					title: err.msg,
					icon: "none"
				})
				// if (loadingText) {
				// 	uni.hideLoading()
				// }
				reject(err)
			}
		})
	});
}

export default baseRequest;